package com.ruoyi.fetv.controller.userController.information;

import com.ruoyi.common.aspect.annotation.AutoDict;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.fetv.controller.userController.information.vo.RequestVO;
import com.ruoyi.fetv.controller.userController.information.vo.RespVO;
import com.ruoyi.fetv.domain.FetvInformation;
import com.ruoyi.fetv.service.IFetvInformationService;
import com.ruoyi.fetv.util.Constant;
import com.ruoyi.fetv.util.Convert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 资讯Controller
 *
 * @author Jack
 * @date 2023-03-07
 */
@Api(value = "/v1/fetv/information", tags = {"用户获取资讯信息"})
@RestController
@RequestMapping("/v1/fetv/information")
@Validated
@AutoDict
public class InformationController extends BaseController {
    @Autowired
    private IFetvInformationService fetvInformationService;

    /**
     * 根据id获得name
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "String", name = "id", required = false)
    })
    @ApiOperation(value = "查询栏目的名字", notes = "查询栏目的名字", httpMethod = "GET")
    @ApiResponse(code = 200, message = "返回对应名称，不报id不存在错误")
    @GetMapping("/name/{id}")
    public String getNameById(@PathVariable(value = "id") String id) {
        return fetvInformationService.getNameById(id);
    }

    /**
     * 查询资讯列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = RequestVO.class, name = "UserInformationRequestVO", value = "资讯参数", required = true)
    })
    @ApiOperation(value = "查询资讯列表", notes = "查询资讯列表", httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo<List<RespVO>> list(RequestVO requestVO) {
        startPage();
        FetvInformation fetvInformation = (FetvInformation) Convert.convert(requestVO, FetvInformation.class);
        fetvInformation.setStatus(Constant.ENABLE);
        fetvInformation.setAuditStatus(Constant.APPROVE);
        List<FetvInformation> list = fetvInformationService.selectFetvInformationList(fetvInformation);
        Convert.covertList(list, FetvInformation.class, RespVO.class);
        return getDataTable(list);
    }
    /**
     * 查询资讯列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = RequestVO.class, name = "UserInformationRequestVO", value = "资讯参数", required = true)
    })
    @ApiOperation(value = "查询资讯标题列表", notes = "查询资讯标题列表", httpMethod = "GET")
    @GetMapping("/listTitle")
    public TableDataInfo<List<RespVO>> listTitle(RequestVO requestVO) {
        startPage();
        FetvInformation fetvInformation = (FetvInformation) Convert.convert(requestVO, FetvInformation.class);
        fetvInformation.setStatus(Constant.ENABLE);
        fetvInformation.setAuditStatus(Constant.APPROVE);
        List<RespVO> list = fetvInformationService.selectFetvInformationTitleList(fetvInformation);
        return getDataTable(list);
    }
    /**
     * 获取资讯详细信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", required = true)
    })
    @ApiOperation(value = "获取资讯详细信息", notes = "获取资讯详细信息", httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = RespVO.class),
            @ApiResponse(code = 500, message = "请不要越权||不存在的ID", response = String.class)
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        FetvInformation data = fetvInformationService.selectFetvInformationById(id);
        if (data == null) {
            throw new ServiceException("不存在的ID");
        }
        if (Objects.equals(data.getStatus(), Constant.ENABLE) && Constant.APPROVE.equals(data.getAuditStatus())) {
            RespVO respVO = (RespVO) Convert.convert(data, RespVO.class);
            return success(respVO);
        }
        throw new ServiceException("请不要越权");
    }

}
